package njust.struts2.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import njust.dao.GoodsDao;
import njust.dao.UserDao;
import njust.domain.Goods;
import njust.domain.User;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private UserDao userDao;
	User user;

	String message = "";

	int goodId;
	GoodsDao goodsDao;

	public String getUserByName() {
		if (user.getUsername().equals(""))
			return "getUserByNameError";
		User my = userDao.getUserByName(user.getUsername());
		if (my != null)
			return "getUserByNameSuccess";
		else
			return "getUserByNameError";
	}

	public String addUser() {
		if (user.getUsername().equals("") || user.getPassword().equals("")) {
			message = "用户名或密码不为空";
			return "addUserFailure";
		} else if (userDao.getUserByName(user.getUsername()) != null) {
			message = "用户已存在";
			return "addUserFailure";
		} else if (userDao.addUser(user).equals("ok")) {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			message = "添加成功";
			return "addUserSuccess";
		} else {
			message = "添加失败";
			return "addUserFailure";
		}
	}

	public String updateUser() {
		if (user.getUsername().equals("") || user.getPassword().equals("")) {
			message = "用户名或密码不为空";
			return "updateUserFailure";
		} else if (userDao.updateUser(user)) {
			message = "更新成功";
			return "updateUserOK";
		} else {
			message = "更新失败";
			return "updateUserFailure";
		}
	}

	public String deleteUserByName(String name) {
		User user = userDao.getUserByName(name);
		if (userDao.deleteUser(user)) {
			message = "删除成功";
			return "deleteUserOK";
		} else {
			message = "删除失败";
			return "deleteUserError";
		}
	}

	public String getAllUser() {
		List userList = userDao.getlistUser();
		if (userList != null) {
			HttpServletRequest re = ServletActionContext.getRequest();
			re.setAttribute("userList", userList);
			return "getallUserOK";
		} else
			return "getallUserFailure";
	}

	public String getUserById(int id) {
		user = userDao.getUserById(id);
		if (user != null)
			return "getUserByIdSuccess";
		else
			return "getUserByIdFailure";

	}

	public String login() {
		if (user.getUsername().equals("") || user.getPassword().equals("")) {
			message = "用户名或密码不为空";
			return "loginError";
		}
		User my = userDao.getUserByName(user.getUsername());
		if (my != null) {
			String pwd = user.getPassword().toString().trim();
			String realpwd = my.getPassword().toString().trim();
			if (pwd.equals(realpwd)) {
				HttpServletRequest request = ServletActionContext.getRequest();
				HttpSession session = request.getSession();
				session.setAttribute("user", my);
				return "loginOk";
			} else {
				message = "密码错误";
				return "loginError";
			}
		} else {
			message = "用户名不存在";
			return "loginError";
		}
	}

	public String logout() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		return "logoutSuccess";
	}

	public GoodsDao getGoodsDao() {
		return goodsDao;
	}

	public void setGoodsDao(GoodsDao goodsDao) {
		this.goodsDao = goodsDao;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public int getGoodId() {
		return goodId;
	}

	public void setGoodId(int goodId) {
		this.goodId = goodId;
	}

}
